﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OT.Data;
using OT.Areas.Admin.Models;
using PagedList;
namespace OT.Areas.Admin.Controllers
{
    public class TestTypeController : Controller
    {
        #region Declaration
        readonly OTEntitiesme OTDb = new OTEntitiesme();
        #endregion

        #region Index
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public JsonResult Index(int pageNumber, int pageSize, string searchString, int pgindex)
        {
            List<TestTypes> Que = ((from c in OTDb.TestTypes
                                    select new TestTypes
                                        {
                                            TestTypeID = c.TestTypeID,
                                            TestType1 = c.TestType1,
                                            CreatedDate = (c.CreatedDate.HasValue) ? c.CreatedDate.Value : DateTime.MinValue,
                                            UpdatedDate = (c.UpdatedDate.HasValue) ? c.UpdatedDate.Value : DateTime.MinValue,
                                        }).OrderBy(o => o.TestType1).ToList<TestTypes>());
            if (!string.IsNullOrEmpty(searchString))
                Que = Que.Where(r => r.TestType1.ToLower().Contains(searchString.ToLower())).ToList();
            int recount =  Que.Count;
            Que = Que.Skip<TestTypes>(pgindex * (pageNumber - 1))
                .Take<TestTypes>(pgindex).ToList<TestTypes>();
            return Json(new
            {
                PageNumber = pageNumber,
                PageSize = pageSize,
                PgIndex = pgindex,
                Data = Que,
                RecordCount = recount
            });
        }
        #endregion

        #region Create
        public ActionResult Create()
        {
            return PartialView();
        }

        [HttpPost]
        public string Create(String TestType)
        {
            string result = "";
            TestType Q = null;
            try
            {
                if (ModelState.IsValid)
                {
                    Q = new TestType();
                    Q.TestType1 = TestType;
                    Q.CreatedDate = DateTime.Now;
                    Q.UpdatedDate = DateTime.Now;
                    OTDb.TestTypes.Add(Q);
                    OTDb.SaveChanges();
                    result = "Sucess";
                }
                else
                {
                    var Citys = OTDb.TestTypes.ToList();
                    result = "NotValid";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }
        #endregion

        #region Edit
        public ActionResult Edit(int TestTypeID)
        {
            TestTypes que = null;
            try
            {
                var categry = OTDb.TestTypes.Where(r => r.TestTypeID == TestTypeID).SingleOrDefault();
                if (categry != null)
                {
                    que = new TestTypes();
                    que.TestTypeID = categry.TestTypeID;
                    que.TestType1 = categry.TestType1;
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return PartialView(que);
        }
        [HttpPost]
        public string Edit(int TestTypeID, String TestType)
        {
            string result = "";
            try
            {
                if (ModelState.IsValid)
                {
                    var que = OTDb.TestTypes.Where(r => r.TestTypeID == TestTypeID).SingleOrDefault();
                    if (que != null)
                    {
                        que.TestType1 = TestType;
                        que.TestTypeID = TestTypeID;
                        que.UpdatedDate = DateTime.Now;
                        OTDb.SaveChanges();
                        result = "Sucess";
                    }
                }
                else
                {
                    result = "NotValid";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }
        #endregion

        #region Delete
        public ActionResult Delete(int TestTypeID)
        {
            TestTypes que = null;
            try
            {
                var categry = OTDb.TestTypes.Where(r => r.TestTypeID == TestTypeID).SingleOrDefault();
                if (categry != null)
                {
                    que = new TestTypes();
                    que.TestTypeID = categry.TestTypeID;
                    que.TestType1 = categry.TestType1;
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return PartialView(que);
        }

        [HttpPost]
        public string Delete(int TestTypeID, string IsDelete)
        {
            string result = "";
            try
            {
                var cat = OTDb.TestTypes.Where(r => r.TestTypeID == TestTypeID).SingleOrDefault();
                if (cat != null)
                {
                    OTDb.TestTypes.Remove(cat);
                    OTDb.SaveChanges();
                    result = "Success";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }
        #endregion
    }
}
